//index.js
//获取应用实例
const app = getApp()
const util = require('../../../utils/util.js')
const QQMapWX = require('../../../utils/qqmap-wx-jssdk.js');
const { Dialog, extend } = require('../../../zanui/index');

let pageObj = {
  data: {
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    resData: undefined
  },
  //事件处理函数
  getData: function () {
    let params = {
      url: '/cp/info/all'
    }
    util.requestData(params).then(res => {
      let app = getApp()
      console.log(app.globalData.userInfo)
      this.showZanDialog({
        content: '调用成功',
        showCancel: true
      }).then(() => {
        console.log(res.data)
        this.setData({
          resData: res.data.list
        })
      })
    })
  },
  postData: function () {
    let params = {
      url: '/cp/message/save',
      method: 'post',
      data: {
        titile: '熊叼毛',
        content: '熊叼毛的激情视频'
      }
    }
    util.requestData(params).then(res => {
      if (res.code === '000000') {
        this.showZanDialog({
          content: '调用成功'
        }).then(() => {
          this.setData({
            resData: JSON.stringify(res)
          })
        })
      }
    })
  },
  getCamare: function () {
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths
      }
    })
  },
  getLocal: function () {
    var that = this
    // 实例化腾讯地图API核心类
    var qqmapsdk = new QQMapWX({
      key: 'GESBZ-6WRWX-6GW4D-7MZMO-WNNT6-MUB4M' // 必填
    });
    //1、获取当前位置坐标
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        //2、根据坐标获取当前位置名称，显示在顶部:腾讯地图逆地址解析
        qqmapsdk.reverseGeocoder({
          location: {
            latitude: res.latitude,
            longitude: res.longitude
          },
          success: function (addressRes) {
            var address = addressRes.result.formatted_addresses.recommend;

            console.log('success')
            that.showZanDialog({
              content: '您的地址是：' + address,
              showCancel: true
            }).then(() => {
              console.log('1111')
            })
          }
        })
      }
    })

  },
  getUserInfo: function (mes) {
    wx.navigateTo({
      url: '../my/my',
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })

  },
  onLoad: function () {
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse) {
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
  },
  getUserInfo: function (e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  }
}

Page(extend({}, Dialog, pageObj))